﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web;
using LeHouse.Server.Dto;
using LeHouse.Server.Service;
using System.IO;
using System.Web.UI.WebControls;
using System.Runtime.Serialization.Formatters.Binary;

namespace LeHouse.Server.WebApi.Controllers
{
    public class SitePictureController : ApiController
    {
        //实例化service
        Collateral_SitePictureService bll = new Collateral_SitePictureService();

        /// <summary>
        /// 图片从前台获取图片转 base64 数据并添加
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/SitePicture/AddSitePicture")]
        public int AddSitePicture([FromBody] Collateral_SitePicture base64)
        {
            //实例化
            string type = base64.T_ptype;//类型赋值
            string base64str = base64.T_Pictureurl.Split(',')[1];//截取base64
            byte[] data = Convert.FromBase64String(base64str);
            string savePath = "/Imgage/"; //虚拟路径，项目中的虚拟路径。一般我们条用这个方法，肯定要把生成的word文档保存在项目的一个文件夹下，以备后续使用
            string path = System.Web.HttpContext.Current.Server.MapPath(savePath); //把相应的虚拟路径转换成物理路径
            if (!System.IO.Directory.Exists(path))
            {
                Directory.CreateDirectory(path);  //如果没有文件就创建
            }
            var pathname = Guid.NewGuid().ToString();//创建一个GUID
            path += pathname + type;//拿到物理路径

            //实例化图片表
            Collateral_SitePicture s = new Collateral_SitePicture();

            if (bll.SitePicturecount(base64) > 0)
            {
                Collateral_SitePicture SitePicture = bll.returnSP_pid(base64.T_Addr);
                s.T_ReportNumber = SitePicture.T_ReportNumber;
                s.T_Picturename = SitePicture.T_Picturename;
                s.T_Picturename = pathname;
                s.T_Pictureurl = "/Imgage/" + pathname + type;//拼接虚拟路径;
                s.T_ptype = base64.T_ptype;
                s.T_Addr = base64.T_Addr;
                FileStream fs = new FileStream(path, FileMode.Create);
                BinaryWriter br = new BinaryWriter(fs);
                br.Write(data, 0, data.Length);
                br.Close();
                fs.Close();
                if (bll.Update(s))
                {
                    return 2;
                }
                else
                {
                    return -2;
                }
            }
            else
            {
                try
                {
                    FileStream fs = new FileStream(path, FileMode.Create);
                    BinaryWriter br = new BinaryWriter(fs);
                    br.Write(data, 0, data.Length);
                    br.Close();
                    fs.Close();
                    s.T_ReportNumber = base64.T_ReportNumber;
                    s.T_Picturename = base64.T_Picturename;
                    s.T_Picturename = pathname;
                    s.T_Pictureurl = "/Imgage/" + pathname + type;//拼接虚拟路径;
                    s.T_ptype = base64.T_ptype;
                    s.T_Addr = base64.T_Addr;
                    if (bll.Insert(s) == true)
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }



        /// <summary>
        /// 图片的回传
        /// </summary>
        /// <param name="P_ReportNumber"></param>
        /// <returns></returns>

        [HttpGet]
        [Route("api/SitePicture/Picture")]
        public List<Collateral_SitePicture> Picture([FromUri] string T_ReportNumber)
        {
            List<Collateral_SitePicture> listPicture = new List<Collateral_SitePicture>();
            List<Collateral_SitePicture> s = bll.PictureShow(T_ReportNumber);
            foreach (var i in s)
            {
                Collateral_SitePicture p = new Collateral_SitePicture();
                p.T_Pictureurl = i.T_Pictureurl;
                p.T_Addr = i.T_Addr;//将要显示的图片添加到这个ID 的div上
                listPicture.Add(p);
            }
            return listPicture;
        }

    }


}

